package com.inspur.security.cbb3.kms.keks.repository;

import com.inspur.security.cbb3.kms.keks.po.KeksBean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface KeksRepository extends JpaRepository<KeksBean, String>, JpaSpecificationExecutor<KeksBean> {

    /**
     * 根据active属性查询
     * @param active 是否是当前主密钥，1-是，0-否
     * @return
     */
    List<KeksBean> findByActive(Integer active);

    /**
     *  根据active和creatorId属性查询
     * @param active
     * @param creatorId
     */
    @Query(value = "from KeksBean kb where kb.active=?1 and kb.creatorId=?2")
    List<KeksBean> findByActiveAndUser(Integer active, String creatorId);

    /**
     * 更新active属性值
     * @param active
     * @param uuid
     */
    @Transactional
    @Modifying
    @Query(value = "update KeksBean kb set kb.active=?1 where kb.uuid=?2")
    void updataActive(Integer active, String uuid);

}
